package net.anumbrella.seaweedfs.config;

import net.anumbrella.seaweedfs.core.FileSource;
import net.anumbrella.seaweedfs.core.FileTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author Mr.Lang
 */
@Configuration
@ConditionalOnProperty(prefix = "seaweedfs",value = "enabled", havingValue = "true")
@EnableConfigurationProperties(SeaWeedProperties.class)
public class SeaWeedAutoConfiguration {

    @Bean
    public FileTemplate fileTemplate(SeaWeedProperties seaWeedProperties) {
        FileSource fileSource = new FileSource();
        fileSource.setHost(seaWeedProperties.getHost());
        fileSource.setPort(seaWeedProperties.getPort());
        fileSource.setMaxConnection(seaWeedProperties.getMaxConnection());
        fileSource.setConnectionTimeout(seaWeedProperties.getConnectionTimeout());
        fileSource.setMasterCluster(seaWeedProperties.isMasterCluster());
        try {
            fileSource.startup();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new FileTemplate(fileSource.getConnection());
    }

}
